package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.framework.utils.UserUtils;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dao.StoreDao;
import com.lcf.system.dto.StoreDTO;
import com.lcf.system.entity.Store;
import com.lcf.system.query.StorePageQuery;
import com.lcf.system.service.StoreService;
import com.lcf.system.vo.StoreVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 店铺(Store)表服务实现类
 *
 * @author lcf
 * @since 2025-05-02 11:55:02
 */
@Service("storeService")
public class StoreServiceImpl extends ServiceImpl<StoreDao, Store> implements StoreService {
    @Override
    public boolean addStore(StoreDTO dto) {
        Store store = BeanUtil.copyProperties(dto, Store.class);
        store.setUserId(UserUtils.getUserId());
        return store.insert();
    }

    @Override
    public boolean deleteStore(long id) {
        return removeById(id);
    }

    @Override
    public boolean updateStore(long id, StoreDTO dto) {
        Store store = BeanUtil.copyProperties(dto, Store.class);
        store.setId(id);
        return store.updateById();
    }

    @Override
    public StoreVO getStore(long id) {
        Store store = getById(id);
        return BeanUtil.copyProperties(store, StoreVO.class);
    }
    @Override
    public StoreVO getUserStore() {
        Store store = Db.lambdaQuery(Store.class)
                .eq(Store::getUserId, UserUtils.getUserId()).one();
        return BeanUtil.copyProperties(store, StoreVO.class);
    }


    @Override
    public PageVO<StoreVO> getStoreList(StorePageQuery query) {
        Page<Store> page = Db.lambdaQuery(Store.class)
                .like(StringUtils.isNotBlank(query.getName()), Store::getName, query.getName())
                .page(query.toPage());
        return new PageVO<>(page, StoreVO.class);
    }
}

